﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sprint1.Models;
using System.Web.Security;
using PagedList;
using PagedList.Mvc;
namespace Sprint1.Controllers
{
    public class CourseController : Controller
    {
        #region Hagar2
        [Authorize]
        [HttpGet]
        public ActionResult AskQuestion()
        {
            return View();
        }
        [Authorize]
        [HttpPost]
        public ActionResult AskQuestion(Question question) //FormCollection formCollection
        {
            using (StudyDatabaseEntities db = new StudyDatabaseEntities())
            {
                string email = HttpContext.User.Identity.Name.ToString();
                Student student = db.Students.Single(s => s.Email == email);
                if ((int?)Session["requestid"] != null)
                {
                    int id = (int)Session["requestid"];
                    if (ModelState.IsValid)
                    {
                        try
                        {
                            question.Date = DateTime.Now;
                            question.Student = student;
                            question.Course = db.Courses.Single(c => c.Id == id);
                            db.Courses.Single(c => c.Id == id).Questions.Add(question);
                            student.Questions.Add(question);
                            db.SaveChanges();
                        }
                        catch (Exception e)
                        {
                            ViewBag.message = e.Message;
                        }
                        //  return RedirectToAction("MyCourses");
                    }
                }
                else
                {
                    return RedirectToAction("MyCourses", "Student");
                }
            }
            return View();
        }
        [Authorize]
        [HttpGet]
        public ActionResult Q_A(int? page)
        {
            if ((int?)Session["requestid"] != null && (int?)Session["requestid"] > 0)
            {
                try
                {
                    int id = (int)Session["requestid"];
                    StudyDatabaseEntities db = new StudyDatabaseEntities();
                    List<Question> Questions = new List<Question>();
                    Questions = db.Questions.Where(q => q.CourseId == id).OrderByDescending(q => q.Date).ToList();
                    foreach (Question q in Questions)
                    {
                        q.Answers = db.Answers.Where(a => a.QuestionId == q.Id).OrderByDescending(a => a.Date).ToList();
                    }
                    return View(Questions.ToPagedList(page ?? 1, 2));
                }
                catch (Exception e)
                {
                    ViewBag.message = e.Message;
                    return ViewBag();
                }
            }
            else
            {
                ViewBag.message = "invalid url ! ! ";
                return View();
                // return RedirectToAction("MyCourses", "Student");
            }


        }
        [Authorize]
        [HttpGet]
        public PartialViewResult PostAnswer()
        {
            return PartialView("_PostAnswer");
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult PostAnswer(Answer answer)
        {
            using (StudyDatabaseEntities db = new StudyDatabaseEntities())
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        string email = HttpContext.User.Identity.Name.ToString();
                        Student student = db.Students.Single(s => s.Email == email);//HE will be a student for awhile
                        answer.Date = DateTime.Now;
                        answer.Student = student;
                        answer.isInstructor = false;
                        if ((int?)Session["QuestionId"] != null && (int?)Session["QuestionId"] > 0)
                        {
                            int id = (int)Session["QuestionId"];
                            answer.QuestionId = id;
                            //answer.Body = CommentBody;
                            db.Answers.Add(answer);
                            db.Questions.Single(q => q.Id == id).Answers.Add(answer);
                            db.Students.Single(q => q.Id == id).Answers.Add(answer);
                            db.SaveChanges();
                            ViewBag.message = "your Comment is posted ";

                        }
                        else RedirectToAction("Q_A");
                    }
                    catch (Exception e)
                    {
                        ViewBag.message = e.Message;

                    }
                }
                List<Answer> answers = db.Answers.Where(a => a.QuestionId == (int)Session["QuestionId"]).OrderByDescending(x => x.Date).ToList();
                return PartialView("_Answer", answers);
            }

        }
        [HttpGet]
        public PartialViewResult SeeAllAnswers()
        {
            //Question question = new Question();
            //question = db.Questions.Single(q => q.Id == (int)Session["QuestionId"]);
            // (int )Session["QuestionId"];
            using (StudyDatabaseEntities db = new StudyDatabaseEntities())
            {
                List<Answer> answers = db.Answers.Where(a => a.QuestionId == (int)Session["QuestionId"]).OrderByDescending(x => x.Date).ToList();
               return PartialView("_Answer", answers);
            }
        }
        [HttpGet]
        public PartialViewResult Last3Answers()
        {
            using (StudyDatabaseEntities db = new StudyDatabaseEntities())
            {
                List<Answer> answers = db.Answers.Where(a => a.QuestionId == (int)Session["QuestionId"]).OrderByDescending(x=> x.Date).Take(3).ToList();
                return PartialView("_Answer", answers);
            }
        }
        [HttpGet]
        public PartialViewResult First3Answers()
        {
            using (StudyDatabaseEntities db = new StudyDatabaseEntities())
            {
                List<Answer> answers = db.Answers.Where(a => a.QuestionId == (int)Session["QuestionId"]).OrderBy(x => x.Date).Take(3).ToList();
                return PartialView("_Answer", answers);
            }
        }
 #endregion

    }

}